package com.swpu.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.swpu.project.common.BaseResponse;
import com.swpu.project.common.DeleteRequest;
import com.swpu.project.common.ErrorCode;
import com.swpu.project.common.ResultUtils;
import com.swpu.project.exception.BusinessException;
import com.swpu.project.model.dto.ebrisnative.EbrisNativeAddRequest;
import com.swpu.project.model.dto.ebrisnative.EbrisNativeQueryRequest;
import com.swpu.project.model.dto.ebrisnative.EbrisNativeUpdateRequest;
import com.swpu.project.model.entity.EbrisNative;
import com.swpu.project.service.EbrisNativeService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 原生岩屑接口
 *
 */
@RestController
@RequestMapping("/ebrisNative")
public class EbrisNativeController {

    @Resource
    private EbrisNativeService ebrisNativeService;



    /**
     * 创建用户
     *
     * @param ebrisNativeAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<String> addEbrisNative(@RequestBody EbrisNativeAddRequest ebrisNativeAddRequest, HttpServletRequest request) {
        if (ebrisNativeAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisNative ebrisNative = new EbrisNative();
        BeanUtils.copyProperties(ebrisNativeAddRequest, ebrisNative);
        boolean result = ebrisNativeService.save(ebrisNative);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(ebrisNative.getNativeId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteEbrisNative(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = ebrisNativeService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param ebrisNativeUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateEbrisNative(@RequestBody EbrisNativeUpdateRequest ebrisNativeUpdateRequest, HttpServletRequest request) {
        if (ebrisNativeUpdateRequest == null || ebrisNativeUpdateRequest.getNativeId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisNative ebrisNative = new EbrisNative();
        BeanUtils.copyProperties(ebrisNativeUpdateRequest, ebrisNative);
        boolean result = ebrisNativeService.updateById(ebrisNative);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取用户
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<EbrisNative> getEbrisNativeById(int id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisNative ebrisNative = ebrisNativeService.getById(id);
        return ResultUtils.success(ebrisNative);
    }

    /**
     * 获取用户列表
     *
     * @param ebrisNativeQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<EbrisNative>> listEbrisNative(EbrisNativeQueryRequest ebrisNativeQueryRequest, HttpServletRequest request) {
        EbrisNative ebrisNativeQuery = new EbrisNative();
        if (ebrisNativeQueryRequest != null) {
            BeanUtils.copyProperties(ebrisNativeQueryRequest, ebrisNativeQuery);
        }
        QueryWrapper<EbrisNative> queryWrapper = new QueryWrapper<>(ebrisNativeQuery);
        List<EbrisNative> ebrisNativeList = ebrisNativeService.list(queryWrapper);
        return ResultUtils.success(ebrisNativeList);
    }

    /**
     * 分页获取用户列表
     *
     * @param ebrisNativeQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<EbrisNative>> listEbrisNativeByPage(EbrisNativeQueryRequest ebrisNativeQueryRequest, HttpServletRequest request) {
        long current = 1;
        long size = 10;
        EbrisNative ebrisNativeQuery = new EbrisNative();
        if (ebrisNativeQueryRequest != null) {
            BeanUtils.copyProperties(ebrisNativeQueryRequest, ebrisNativeQuery);
            current = ebrisNativeQueryRequest.getCurrent();
            size = ebrisNativeQueryRequest.getPageSize();
        }
        QueryWrapper<EbrisNative> queryWrapper = new QueryWrapper<>(ebrisNativeQuery);
        Page<EbrisNative> ebrisNativePage = ebrisNativeService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(ebrisNativePage);
    }

}
